package br.ufrj.neg.reuniao.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.constants.General;
import br.ufrj.neg.reuniao.bean.EncaminhamentoBean;
import br.ufrj.neg.reuniao.bean.ReuniaoBean;
import br.ufrj.neg.util.Data;


public class ExcluirParticipanteReuniaoAction extends VelocityViewServlet {

	private static final long serialVersionUID = 1L;
	//private static Logger log = Logger.getLogger("rootLogger");
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		try{
			HttpSession session = request.getSession(false);
			ReuniaoBean rb = null;
			synchronized (session) {
				rb = (ReuniaoBean) session.getAttribute("ReuniaoBean");
			}
			/*
			 * Pega os parametros passados
			 */
			
			String descReuniao = General.getParameterUTF8(request, "descricao");
			String local = General.getParameterUTF8(request, "local");
			String data = request.getParameter("data");
			String hora = request.getParameter("hora");
			int tipoReuniao = Integer.parseInt(request
					.getParameter("tipoReuniao"));
			String encaminhamentoReuniao = General.getParameterUTF8(request, "encaminhamento");
			String temp = request.getParameter("idEncaminhamento") ;
			int idEncaminhamento = 0;
			if(temp != null && !temp.equals(""))
				idEncaminhamento = Integer.parseInt(request
					.getParameter("idEncaminhamento"));
			
			rb.setDescricao(descReuniao);
			rb.setLocal(local);
			if ((data != null && data.length() > 0)){
				if(hora == null || hora.length() == 0)
					rb.setData(new Data(data));
				else
					rb.setData(new Data(data, hora));
			}
			else{
				rb.setData(null);
			}
			rb.setHora(hora);
			rb.setTipoReuniao(tipoReuniao);
			EncaminhamentoBean eb = new EncaminhamentoBean();
			eb.setId(idEncaminhamento);
			eb.setDescricao(encaminhamentoReuniao);
			rb.setEncaminhamento(eb);

			
			String indice = request.getParameter("indParticipante");
			rb.getParticipantes().remove(Integer.valueOf(indice).intValue());
			for(int i = Integer.valueOf(indice); i < rb.getParticipantes().size(); i++){
				rb.getParticipantes().get(i).setIndice(i);
			}
			
			synchronized (session) {
				session.setAttribute("ReuniaoBean", rb);
			}

			request.getRequestDispatcher("reuniao.editar").forward(request, response);
			
		}catch(Exception e){
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request, response);
		}
	}

}
